//turret.txt - Simple script for turrets
// Cell 0 - ability
//Cell 2,3 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.

begincreaturescript;

short i,target;
short last_abil;
short ae_range = 8;
short sizzle = 0;
short last_abil;
short ae_range = 8;
short summon = 0;
short debuffed = 0;

body;

beginstate INIT_STATE;
	last_abil = get_current_tick();
	last_abil = get_current_tick();
	
	set_level(ME,26);
	set_boss_level(ME,2);
	
	if (gf(100,19) > 1)
		set_attitude(ME,4);
	break;

beginstate DEAD_STATE;
	set_flag(52,13,1);
	sf(52,7,0);
	if (gf(100,19) == 0) {
		sf(100,19,1);
		award_party_xp(200,20);
		begin_talk_mode(6);
		}
break;

beginstate START_STATE; 
	if ((sizzle > 0) && (get_char_status(ME,20) == 0)) {
		force_char_status(ME,20,10);
		force_char_status(ME,6,10);
		}
	if ((get_nearest_party_char(8) >= 0) && (gf(52,7) == 1) && (gf(52,9) > 32) && (sizzle == 0) && (gf(52,16) + gf(52,17) + gf(52,18) > 0)) {
		sizzle = 1;
		summon = 3;
		set_level(ME,12);
		set_boss_level(ME,0);
		begin_talk_mode(65);
		}
		
	if (who_shot_me() >= 0) {

		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}


		
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if ((sizzle > 0) && (get_char_status(ME,20) == 0)) {
		force_char_status(ME,20,10);
		force_char_status(ME,6,10);
		}
	if ((get_nearest_party_char(8) >= 0) && (gf(52,7) == 1) && (gf(52,9) > 32) && (sizzle == 0) && (gf(52,16) + gf(52,17) + gf(52,18) > 0)) {
		sizzle = 1;
		summon = 3;
		set_level(ME,12);
		set_boss_level(ME,0);
		begin_talk_mode(65);
		}


	if (target_ok() == FALSE)
		set_state(START_STATE);
	
	if ((summon == 0) && (get_health(ME) < (get_max_health(ME) * 3) / 4)) {
		summon = 1;
		run_char_animation(2,1,50);	
		pc_heard_sound_delay(136,100);						
		print_named_str(ME,"opens one of the cabinets along the north wall.");

		spawn_creature(19);
		place_particle_num(27,10,7,10);
		set_boss_level(27,2);
		kill_object(89,0);
		
		end();
		}
	else if ((summon == 1) && (get_health(ME) < (get_max_health(ME) * 2) / 4)) {
		summon = 2;
		run_char_animation(2,1,50);	
		pc_heard_sound_delay(136,100);						
		print_named_str(ME,"opens one of the cabinets along the north wall.");

		spawn_creature(20);
		place_particle_num(28,10,7,10);
		set_boss_level(28,2);
		kill_object(90,0);
		
		end();
		}
	else if ((summon == 2) && (get_health(ME) < (get_max_health(ME) * 1) / 4)) {
		summon = 3;
		run_char_animation(2,1,50);	
		pc_heard_sound_delay(136,100);						
		print_named_str(ME,"opens one of the cabinets along the north wall.");

		spawn_creature(21);
		place_particle_num(29,10,7,10);
		set_boss_level(29,2);
		kill_object(91,0);
		
		end();
		}
		
	if ((get_attitude(ME) >= 10) && (is_combat()) && 
	  (tick_difference(last_abil,get_current_tick()) > 1)) {
		run_char_animation(2,1,35);	

		  	place_particle_num(ME,1,4,8);
	  		create_missile_spiral(155,40,ae_range,2);
		
		if ((debuffed == 0) && (get_health(ME) < (get_max_health(ME) * 2) / 4)) {
			debuffed = 1;
			print_named_str(ME,"radiates a cloud of static energy.");
			status_nearby(-40,ae_range,9,0);
			status_nearby(-40,ae_range,11,0);
			status_nearby(-40,ae_range,12,0);
			status_nearby(-40,ae_range,13,0);
			status_nearby(-40,ae_range,14,0);
			status_nearby(-40,ae_range,15,0);
			status_nearby(-40,ae_range,16,0);
			status_nearby(-40,ae_range,18,0);
			status_nearby(-40,ae_range,19,0);
			}
			else {
				print_named_str(ME,"radiates a cloud of searing energy.");
				damage_nearby(get_ran(get_level(ME) + get_attack_bonus(ME),1,10),ae_range,1,0);
				status_nearby(3,ae_range,20,0);
				}
				
			pc_heard_sound_delay(179,250);						

		last_abil = get_current_tick();
		end();
		}
		

	do_attack();

	turret_heal();
break;

beginstate TALKING_STATE;
	if (gf(100,19) > 1)
		begin_talk_mode(7);
		else if (gf(52,7) > 0)
			print_str_color("The Synthesis Mind is too distracted to talk to you.",2);
			else begin_talk_mode(90);
break;